# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_memory.py
#日期：2019-12-16
#备注：查看函数运行时占用的内存数量，垃圾回收机制
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import os
import psutil


def show_memory_info(hint):
    pid = os.getpid()
    p = psutil.Process(pid)
    info = p.memory_full_info()
    memory = info.uss /1024./1024
    print('{} memory used: {} MB'.format(hint,memory))


# 在函数返回后，局部变量的引用会注销掉，
def test():
    show_memory_info('initial')
    a = [i for i in range(1000000)]
    show_memory_info('after a created')


# 声明为全局变量，对象不会被垃圾回收掉，依然占用大量内存
def g_test():
    show_memory_info('initial')
    global a
    a = [i for i in range(1000000)]
    show_memory_info('after a created')


# return 返回
def r_test():
    show_memory_info('initial')
    a = [i for i in range(1000000)]
    show_memory_info('after a created')
    return a


if __name__ == '__main__':
    #test()
    #g_test()
    t = r_test()
    show_memory_info('finished')




